package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.bo.AdminCouponListBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.CouponListVo;
import com.cskaoyan.bean.vo.CouponReadVo;
import com.cskaoyan.bean.vo.CouponUpdateVo;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.utils.CodeUtil;
import com.cskaoyan.utils.ValidationUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 优惠券管理
 *
 * @author Fyy
 * @since 2022/12/29 15:16
 */

@RestController
@RequestMapping("admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    /**
     * 优惠券管理页面
     * @param adminCouponListBO
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(AdminCouponListBO adminCouponListBO) {
        //获取查询的数据
        CommonData<MarketCoupon> data = couponService.queryAll(adminCouponListBO);
        //需要返回给客户端的数据
        CommonData<CouponListVo> realData = new CommonData<>();
        //通过查询数据给需要返回给客户端的数据赋值
        BeanUtils.copyProperties(data,realData);
        List<MarketCoupon> list = data.getList();
        List<CouponListVo> listVos = new ArrayList<>();
        for (MarketCoupon marketCoupon : list) {
            CouponListVo couponListVo = new CouponListVo();
            BeanUtils.copyProperties(marketCoupon,couponListVo);
            listVos.add(couponListVo);
        }
        realData.setList(listVos);
        BaseRespVo respVo = BaseRespVo.ok(realData);
        respVo.setErrmsg("成功");
        return respVo;
    }

    /**
     * 新建优惠券
     * @param marketCoupon
     * @param bindingResult
     * @return
     * @throws ValidationException
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Valid MarketCoupon marketCoupon, BindingResult bindingResult) throws ValidationException {
        //参数校验
        ValidationUtil.valid(bindingResult);
        //添加时间
        marketCoupon.setAddTime(new Date());
        //修改时间
        marketCoupon.setUpdateTime(new Date());
        //给优惠兑换码赋值
        marketCoupon.setCode(CodeUtil.getCouponCode());
        couponService.create(marketCoupon);
        //给返回值赋值
        CouponUpdateVo data = new CouponUpdateVo();
        BeanUtils.copyProperties(marketCoupon,data);
        BaseRespVo respVo = BaseRespVo.ok(data);
        respVo.setErrmsg("成功");
        return respVo;
    }

    /**
     * 修改优惠券
     * @param marketCoupon
     * @param bindingResult
     * @return
     * @throws ValidationException
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Valid MarketCoupon marketCoupon, BindingResult bindingResult) throws ValidationException {
        //参数校验
        ValidationUtil.valid(bindingResult);
        marketCoupon.setUpdateTime(new Date());
        couponService.update(marketCoupon);
        //给返回值赋值
        CouponUpdateVo data = new CouponUpdateVo();
        BeanUtils.copyProperties(marketCoupon,data);
        BaseRespVo respVo = BaseRespVo.ok(data);
        respVo.setErrmsg("成功");
        return respVo;
    }

    /**
     * 删除优惠券
     * @param marketCoupon
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon) {
        couponService.delete(marketCoupon);
        BaseRespVo respVo = BaseRespVo.ok();
        respVo.setErrmsg("成功");
        return respVo;
    }

    /**
     * 优惠券详情页面
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        MarketCoupon marketCoupon = couponService.query(id);
        //给返回值赋值
        CouponReadVo couponReadVo = new CouponReadVo();
        BeanUtils.copyProperties(marketCoupon, couponReadVo);
        return BaseRespVo.ok(couponReadVo, "成功");
    }

    /**
     * 用户持有优惠券页面
     * @param basePageInfo
     * @param couponId
     * @param userId
     * @param status
     * @return
     */
    @RequestMapping("listuser")
    public BaseRespVo listUser(BasePageInfo basePageInfo, Integer couponId, Integer userId, Short status) {
        CommonData<MarketCouponUser> data = couponService.listUser(basePageInfo, couponId, userId, status);
        return BaseRespVo.ok(data, "成功");
    }

}
